use clap::Parser;

use serialport::{DataBits, FlowControl, Parity, StopBits};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(short, long, required = true)]
    port: String,

    #[arg(short, long, default_value_t = 9600, help = "baud_rate")]
    baud_rate: u32,

    #[arg(short, long, default_value = "8", value_parser = ["5", "6", "7", "8"],  help = "data_bits")]
    data_bits: String,

    #[arg(long, default_value = "1", value_parser = ["1", "2"] ,  help = "stop_bits")]
    stop_bits: String,

    #[arg(long, default_value = "none", value_parser = ["none", "odd", "even"], help = "parity")]
    parity: String,

    #[arg(long, default_value = "none",
                value_parser = ["none", "software", "hardware"],
                help = "flow_control")]
    flow_control: String,
}

fn main() {
    let args = Args::parse();

    let data_bits = match args.data_bits.as_str() {
        "5" => DataBits::Five,
        "6" => DataBits::Six,
        "7" => DataBits::Seven,
        "8" => DataBits::Eight,
        _ => unreachable!(),
    };

    let stop_bits = match args.stop_bits.as_str() {
        "1" => StopBits::One,
        "2" => StopBits::Two,
        _ => unreachable!(),
    };

    let parity = match args.parity.as_str() {
        "none" => Parity::None,
        "odd" => Parity::Odd,
        "even" => Parity::Even,
        _ => unreachable!(),
    };
    let flow_control = match args.flow_control.as_str() {
        "none" => FlowControl::None,
        "software" => FlowControl::Software,
        "hardware" => FlowControl::Hardware,
        _ => unreachable!(),
    };

    // 打印配置信息
    println!("Port Name: {}", args.port);
    println!("Baud Rate: {}", args.baud_rate);
    println!("Data Bits: {:?}", data_bits);
    println!("Stop Bits: {:?}", stop_bits);
    println!("Parity: {:?}", parity);
    println!("Flow Control: {:?}", flow_control);
}
